package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.BusinessDao;
import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderDetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.BusinessDaoImpl;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderDetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-27 14:52
 */
public class OrdersServiceImpl implements OrdersService {
    /**
     * 1. OrdersController/createOrders
     * 参数：userId、businessId、daId、orderTotal
     * 返回值：int（订单编号）
     * 功能：根据用户编号、商家编号、订单总金额、送货地址编号向订单表中添加一条记录，
     * 并获取自动生成的订单编号，
     * 然后根据用户编号、商家编号从购物车表中查询所有数据，批量添加到订单明细表中，
     * 然后根据用户编号、商家编号删除购物车表中的数据。
     */
    @Override
    public int saveOrders(String userId, int businessId, int daId) {
        double priceTotle=0;
        BusinessDao businessDao = new BusinessDaoImpl();
        CartDao cartDao = new CartDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderDetailetDao orderDetailetDao = new OrderDetailetDaoImpl();
        try{

            //1、查询当前用户购物车中当前商家的所有食品
            List<Object> list = cartDao.listCart(userId, businessId);
//            List<Cart> cartList=(Cart)list;
            //2、查询商家信息（需要使用商家的配送费信息）
            Business businessById = businessDao.getBusinessById(businessId);
            //3、获取订单总额	 //累计所有食品总价格	+商家配送费
            Cart cart = null;
            for(int i=0;i<list.size();i++){
                cart=(Cart) list.get(i);
                //购物车中对应商家对应的食品单价 *数量
                priceTotle+=cart.getFood().getFoodPrice()*cart.getQuantity();

            }
            //加上配送费
            priceTotle+=businessById.getDeliveryPrice();

            //4、创建订单，并获取订单编号
            int ORID;//订单编号
            ORID= ordersDao.saveOrders(userId, businessId, daId, priceTotle);
            //5、处理相关数据：获取订单明细集合

            List<OrderDetailet> list1= new ArrayList<>();

            for (int i = 0; i < list.size(); i++) {
                cart=(Cart) list.get(i);
                OrderDetailet orderDetailet=new OrderDetailet();
                orderDetailet.setOrderId(ORID);
                orderDetailet.setFoodId(cart.getFoodId());
                orderDetailet.setQuantity(cart.getQuantity());
                list1.add(orderDetailet);
            }


            //6、批量添加订单明细
            int i = orderDetailetDao.saveOrderDetailetBatch(list1);



            //7、清空当前用户购物车中当前商家的所有食品
            Cart cart1=new Cart();
            cart1.setUserId(userId);
            cart1.setBusinessId(businessId);
            //根据用户编号、商家编号、食品编号
            int i1 = cartDao.removeCart(cart1);
            //8. DBUtill.commitTransaction() 提交事务 回滚事务
            return ORID;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }


//        try {
//            int i = dao.saveOrders(userId, businessId, daId, orderTotal);
//            return i;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return 0;
        return 0;
    }

    /**
     * 2. OrdersController/getOrdersById
     * 参数：orderId
     * 返回值：orders对象（包括多对一：商家信息；    一对多：订单明细信息）
     * 功能：根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息
     **/
    @Override
    public Orders getOrdersById(int orderId) {
        OrdersDao dao = new OrdersDaoImpl();
        try {
            Orders ordersById = dao.getOrdersById(orderId);
            return ordersById;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        OrdersDao dao = new OrdersDaoImpl();
        try {
            List<Orders> list = dao.listOrdersByUserId(userId);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
